﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ControlsLib
{
    [Serializable]
    public partial class AnagraficaManager2<T> : AnagraficaManagerBase where T : class, new()
    {
        public event EventHandler<CustomEventArgs<T>> ButtonClicked;

        ButtonsBar<T> m_buttonsBar = null;
        bool m_canModify = false;

        public new BindingSource BindingSource { get { return base.BindingSource; } }

        public bool CanModify
        {
            get { return m_canModify; }
            set 
            { 
                m_canModify = value;
            }
        }
        
        public AnagraficaManager2()
        {
            InitializeComponent();

            if (!this.DesignMode)
            {
                m_buttonsBar = new ButtonsBar<T>();
                m_buttonsBar.Dock = DockStyle.Bottom;
                this.Controls.Add(m_buttonsBar);

                m_buttonsBar.ButtonClicked += new EventHandler<CustomEventArgs<T>>(m_buttonsBar_ButtonClicked);
                this.dataGridView1.SelectionChanged += new EventHandler(dataGridView1_SelectionChanged);
                this.dataGridView1.RowHeaderMouseClick += new DataGridViewCellMouseEventHandler(RowHeaderMouseClick);
                this.dataGridView1.RowHeaderMouseDoubleClick += new DataGridViewCellMouseEventHandler(RowHeaderMouseClick);

                this.SelectedType = typeof(T);
                this.BindingSource.DataSource = typeof(T);

                //CreateInputInterface(typeof(T));

                ControlsLib.Gridview.Filtering.SearchExtender se = new Gridview.Filtering.SearchExtender(this);
                se.AltFPressed += new EventHandler(se_AltFPressed);
                se.CtrlFPressed += new EventHandler(se_CtrlFPressed);
            }
        }

        void se_CtrlFPressed(object sender, EventArgs e)
        {
            MessageBox.Show("CRTL F pressed");
        }

        void se_AltFPressed(object sender, EventArgs e)
        {
            MessageBox.Show("ALT F pressed");
        }

        void m_buttonsBar_ButtonClicked(object sender, CustomEventArgs<T> e)
        {
            if (e.Action == Actions.New)
            {
                if (this.BindingSource.DataSource == null)
                {
                    this.BindingSource.DataSource = typeof(T);
                }
                //this.propertyGrid1.SelectedObject = new T();

                m_buttonsBar.Item = this.BindingSource.DataSource as T;
            }

            if (ButtonClicked != null)
            {
                this.BindingSource.EndEdit();
                ButtonClicked(sender, e);
            }
        }

        void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            m_buttonsBar.Item = (T)((DataGridView)sender).CurrentRow.DataBoundItem;
            m_buttonsBar.CurrentAction = (m_canModify ? Actions.Delete | Actions.Modify : Actions.Delete);

            this.BindingSource.DataSource = m_buttonsBar.Item;
        }

        void RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            m_buttonsBar.Item = (T)((DataGridView)sender).CurrentRow.DataBoundItem;
            m_buttonsBar.CurrentAction = (m_canModify ? Actions.Delete | Actions.Modify : Actions.Delete);

            this.BindingSource.DataSource = m_buttonsBar.Item;
        }

        private void CreateInputInterface(Type data)
        {
            int step = 15;
            int leftOffset = 5;
            this.BindingSource.DataSource = new T();
            foreach (System.Reflection.PropertyInfo pi in data.GetProperties())
            {
                //Label l = new Label();
                Label l = new Label();
                l.Location = new System.Drawing.Point(leftOffset, step);
                l.Text = string.Format("{0}:", pi.Name);

                TextBox txt = new TextBox();
                //Control txt = dfch.GetSpecificControl(pi.PropertyType);
                txt.Location = new System.Drawing.Point(leftOffset + l.Width + 5, step);

                // DataBinding...
                ControlBindingsCollection OldDataBindings = txt.DataBindings;
                //RaiseComponentChanging(TypeDescriptor.GetProperties(typeof(Binding))["DataBindings"]);
                txt.DataBindings.Add(new Binding("Text", this.BindingSource, pi.Name));
                //RaiseComponentChanged(TypeDescriptor.GetProperties(typeof(Binding))["DataBindings"], OldDataBindings, txt.DataBindings);

                //
                step += l.Size.Height;

                //
                this.gbDetails.Controls.Add(l);
                this.gbDetails.Controls.Add(txt);
            }
        }
    }
}
